package com.example.administrator.szb.tinkerutil;

import android.app.IntentService;
import android.content.Intent;

import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.http.okhttps.DownloadUtil;
import com.example.administrator.szb.util.AppUtils;
import com.example.administrator.szb.util.QTLog;
import com.tencent.tinker.lib.tinker.TinkerInstaller;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by stone on 2018/2/28.
 */

public class DownLoadTinker  extends IntentService {
    public static boolean isStart = false;
    @Override
    public void onCreate() {
        super.onCreate();
        isStart = true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isStart = false;

    }

    public DownLoadTinker() {
        super("DownLoadTinker");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Map<String,Object> params = new HashMap<>();
        params.put("version", AppUtils.getVersionCode(this));
        params.put("code", HttpUtil.getParam());
        params.put("app", "android");
        HttpUtils.post(null, URLAddress.URL_BD, params, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                downStart(str);
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });



    }
    private  void downStart(String url){
        DownloadUtil.get().download(url, "szbfilebuding", new DownloadUtil.OnDownloadListener() {
            @Override
            public void onDownloadSuccess(String path) {
                QTLog.e("补丁下载完成"+path);
                TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(),path);
            }
            @Override
            public void onDownloading(int progress) {

            }
            @Override
            public void onDownloadFailed() {

            }
        });
    }


}
